Skip to content

fix: multi-table DELETE+LIMIT syntax error and inaccurate index comment in cleanup_orphan_stats - #115

Merged
XuPeng-SH merged 3 commits into
mainfrom
copilot/database-operations-and-performance
Mar 24, 2026
Merged

fix: multi-table DELETE+LIMIT syntax error and inaccurate index comment in cleanup_orphan_stats#115
XuPeng-SH merged 3 commits into
mainfrom
copilot/database-operations-and-performance

Conversation

Copilot AI commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

Two issues in the batched cleanup_orphan_stats rewrite and one misleading comment introduced by the previous perf commit.

Bug fix: multi-table DELETE with LIMIT is invalid MySQL/MatrixOne syntax

DELETE alias FROM t1 LEFT JOIN t2 … LIMIT n is rejected at runtime — MySQL forbids ORDER BY/LIMIT on multi-table deletes. Every other batched DELETE in the file is single-table for exactly this reason.

Replaced with the standard two-step pattern used elsewhere:

-- Step 1: SELECT up to 1000 orphan IDs (LIMIT valid on SELECT)
SELECT s.memory_id
FROM mem_memories_stats s
LEFT JOIN mem_memories m ON s.memory_id = m.memory_id
WHERE m.memory_id IS NULL
LIMIT 1000;

-- Step 2: single-table DELETE by PK — no LIMIT restriction
DELETE FROM mem_memories_stats WHERE memory_id IN (?, …);

mem_memories_stats has memory_id as its PRIMARY KEY (one row per memory), so the SELECT never returns duplicates and the loop terminates correctly.

Comment fix: idx_memories_user_observed scope

The original comment claimed the index also accelerates archive_stale_working. It doesn't — that function uses TIMESTAMPDIFF(HOUR, observed_at, NOW()) > ?, which wraps observed_at in a function and prevents a B-tree range scan. The index only helps health_capacity()'s direct range predicate observed_at >= NOW() - INTERVAL 30 DAY. Comment corrected accordingly.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Discuss database operations and potential performance issues perf(storage): add missing indexes and cap unbounded DB operations Mar 24, 2026
Copilot AI requested a review from XuPeng-SH March 24, 2026 14:21
…leanup_orphan_stats

Co-authored-by: XuPeng-SH <39627130+XuPeng-SH@users.noreply.github.com>
Agent-Logs-Url: https://github.com/matrixorigin/Memoria/sessions/8109f997-3693-4df7-9c8e-3484b74b92d4
Copilot AI changed the title perf(storage): add missing indexes and cap unbounded DB operations perf(storage): add missing indexes, cap unbounded ops, fix multi-table DELETE+LIMIT bug Mar 24, 2026
Copilot AI changed the title perf(storage): add missing indexes, cap unbounded ops, fix multi-table DELETE+LIMIT bug fix: multi-table DELETE+LIMIT syntax error and inaccurate index comment in cleanup_orphan_stats Mar 24, 2026
@XuPeng-SH
XuPeng-SH marked this pull request as ready for review March 24, 2026 15:59
@XuPeng-SH
XuPeng-SH enabled auto-merge (squash) March 24, 2026 15:59
@XuPeng-SH
XuPeng-SH disabled auto-merge March 24, 2026 16:04
@XuPeng-SH
XuPeng-SH merged commit 8553bf0 into main Mar 24, 2026
8 checks passed
@XuPeng-SH
XuPeng-SH deleted the copilot/database-operations-and-performance branch March 24, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants